package com.zaoniao.enstudy.domain.service;

import com.zaoniao.enstudy.domain.entity.User;
import com.zaoniao.enstudy.domain.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户相关业务.
 * @author llb666
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserService {

    private final UserRepository userRepository;

    /**
     * 保存或修改用户信息.
     * @param user 用户信息.
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveOrUpdate(User user) {
        User userByOpenId = userRepository.getUserByOpenId(user.getOpenId());
        if (userByOpenId == null) {
            userRepository.saveUser(user);
        }else {
            userRepository.updateUser(user);
        }
    }

}
